import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_hkzf/utils/common_toast.dart';
import 'package:flutter_hkzf/utils/dio_http.dart';
import 'package:flutter_hkzf/utils/string_is_null_or_empty.dart';
import 'package:fluttertoast/fluttertoast.dart';

class RegisterPage extends StatefulWidget {
  RegisterPage({Key? key}) : super(key: key);

  @override
  State<RegisterPage> createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  //写三个控制器
  var usernameController = TextEditingController();
  var passwordController = TextEditingController();
  var repeatPasswordController = TextEditingController();

  //注册方法
  _registerHandler() async {
    //获取注册信息
    var username = usernameController.text;
    var password = passwordController.text;
    var repeatPassword = repeatPasswordController.text;

    if (stringIsNullOrEmpty(username) || stringIsNullOrEmpty(username)) {
      CommonToast.showToast('用户名或密码不能为空!');
      return;
    }
    if (password != repeatPassword) {
      CommonToast.showToast('两次输入密码不一致');
      return;
    }
    const url = '/register';
    var params = {'username': username, 'password': password};
    var res = await DioHttp.of(context).post(url, params);
    // 解析
    var resString = json.decode(res.toString());
    print(resString);
    bool status = resString['success'];
    String description = resString['msg'] ?? '内部错误';
    CommonToast.showToast(description);
    if (status) {
      Navigator.pushReplacementNamed(context, 'login');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("注册")),
      body: SingleChildScrollView(
          child: Padding(
              padding: const EdgeInsets.all(20),
              child: Column(
                children: [
                  TextField(
                    controller: usernameController,
                    decoration: const InputDecoration(
                        labelText: '账号', hintText: "请输入用户名"),
                  ),
                  const SizedBox(
                    height: 16,
                  ),
                  TextField(
                    controller: passwordController,
                    obscureText: true,
                    decoration: const InputDecoration(
                      labelText: '密码',
                      hintText: "请输入密码",
                    ),
                  ),
                  const SizedBox(
                    height: 16,
                  ),
                  TextField(
                    controller: repeatPasswordController,
                    obscureText: true,
                    decoration: const InputDecoration(
                        labelText: '确认密码', hintText: "请确认密码"),
                  ),
                  const SizedBox(height: 24),
                  Container(
                    width: double.infinity,
                    height: 40,
                    child: ElevatedButton(
                      child: const Text(
                        "注册",
                        style: TextStyle(fontSize: 26),
                      ),
                      onPressed: () {
                        _registerHandler();
                      },
                    ),
                  ),
                  const SizedBox(
                    height: 24,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const Text("已有账号"),
                      const SizedBox(height: 20),
                      TextButton(
                          onPressed: () {
                            Navigator.pushReplacementNamed(context, 'login');
                          },
                          child: const Text("去登录吧~",
                              style: TextStyle(color: Colors.teal)))
                    ],
                  )
                ],
              ))),
    );
  }
}
